<template>
  <div class="app">
    <div class="search-bar">
      <el-form :model="searchData" label-width="80px" style="padding: 0 20px;">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="公司名称" style="margin-bottom: 5px;">
              <el-select v-model="searchData.cmhkId"
                size="medium"
                filterable
                clearable
                class="select-item"
                remote
                reserve-keyword
                :remote-method="remoteMethod"
                @change="selectCompany"
                placeholder="请输入字段搜索">
                <el-option
                  v-for="item in companyNameOption"
                  :key="item.code"
                  :label="item.value"
                  :value="item.code">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <div style="margin-top: 3px;">
              <el-button icon="el-icon-search" type="primary" size="small" @click="handleSearch">查询</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="add-form">
      <el-button icon="el-icon-plus" v-show="tableOption.isShowOperate" type="primary" size="small" @click="handleAdd">新增</el-button>
    </div>
    <div ref="ref" :class="tableOption.isShowOperate?'table-box':'table-box2'">
      <TableComponent :columns="financeColumns" :height="height" :data="tableData" @change="onTableChange" :tableOption="tableOption" :page-option="searchData.page" :stripe="stripe" :total="total">
        <template slot-scope="scope">
          <el-button @click="editRow(scope.scope.row)" type="text" size="mini">编辑</el-button>
        </template>
      </TableComponent>
    </div>
    <el-dialog :title="title" :visible.sync="dialogVisibleAdd" width="60%">
      <div class="dialog-box">
        <el-form ref="form" :model="addForm" label-width="190px" :rules="rules" label-position="left" style="padding: 0 20px;">
          <el-row :gutter="30">
            <el-col :span="12">
              <el-form-item label="财务年度" prop="financeYear" style="margin-bottom: 12px; margin-left: -9px">
                <el-date-picker :picker-options="pickerOptions" size="small" style="width: calc(100% - 9px); margin-left: 9px" v-model="addForm.financeYear" type="year" placeholder="选择年" class="select-item" value-format="yyyy"></el-date-picker>
              </el-form-item>
              <el-form-item label="财务报表币种" prop="currency" style="margin-bottom: 12px; margin-left: -9px">
                <el-select v-model="addForm.currency" size="small" style="width: calc(100% - 9px); margin-left: 9px" class="select-item">
                  <el-option
                    v-for="item in currencyOption"
                    :key="item.index"
                    :label="item.description"
                    :value="item.code">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="营业收入" prop="income" style="margin-bottom: 12px;">
                <el-input v-model="addForm.income" size="small"></el-input>
              </el-form-item>
              <el-form-item label="归属于母公司所有者净利润" prop="profitParentCompany" style="margin-bottom: 12px;">
                <el-input v-model="addForm.profitParentCompany" size="small"></el-input>
              </el-form-item>
              <el-form-item label="负债总额" prop="liabilitiesAll" style="margin-bottom: 12px;">
                <el-input v-model="addForm.liabilitiesAll" size="small"></el-input>
              </el-form-item>
              <el-form-item label="员工人数" prop="employeeNumber" style="margin-bottom: 12px;">
                <el-input v-model="addForm.employeeNumber" size="small"></el-input>
              </el-form-item>
              <el-form-item label="资本回报率(%)" prop="capitalReturnRate" style="margin-bottom: 12px;">
                <el-input v-model="addForm.capitalReturnRate" size="small"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="利润总额" prop="profit" style="margin-bottom: 12px;">
                <el-input v-model="addForm.profit" size="small"></el-input>
              </el-form-item>
              <el-form-item label="财务指标来源" prop="indexFrom" style="margin-bottom: 12px;">
                <el-select v-model="addForm.indexFrom" size="small" class="select-item">
                  <el-option
                    v-for="item in profitOption"
                    :key="item.index"
                    :label="item.value"
                    :value="item.code">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="填报途径" prop="fillType" style="margin-bottom: 12px;">
                <el-select v-model="addForm.fillType" size="small" class="select-item">
                  <el-option
                    v-for="item in fillTypeOption"
                    :key="item.index"
                    :label="item.value"
                    :value="item.code">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="归属于母公司所有者权益" prop="rightParentCompany" style="margin-bottom: 12px;">
                <el-input v-model="addForm.rightParentCompany" size="small"></el-input>
              </el-form-item>
              <el-form-item label="资产总额" prop="assetAll" style="margin-bottom: 12px;">
                <el-input v-model="addForm.assetAll" size="small"></el-input>
              </el-form-item>
              <el-form-item label="中方员工人数" prop="chiEmployeeNumber" style="margin-bottom: 12px;">
                <el-input v-model="addForm.chiEmployeeNumber" size="small"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <span slot="footer">
        <el-button @click="handleCancle">取 消</el-button>
        <el-button type="primary" @click="handleSubmit('form')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import TableComponent from '@/components/Table/table'
import { queryFinanceList, querySingleFinance, editFinanceList, addFinanceList, queryCompanyByName, Currency, indexFrom } from '@/api/modules/propertyRight.js'
import { financeColumns } from './index.js'
import { checkNumber } from '@/utils/pattern.js'
export default {
  components: {
    TableComponent
  },
  data() {
    return {
      searchData: {
        cmhkId: '',
        page: {
          pageNum: 1,
          pageSize: 10
        }
      },
      financeColumns,
      tableData: [],
      height: null,
      title: '',
      total: 0,
      tableOption: {
        isShowOperate: true,
        operateWidth: '100px'
      },
      stripe: true,
      dialogVisibleAdd: false,
      addForm: {
        financeYear: '',
        currency: '',
        income: null,
        profitParentCompany: null,
        liabilitiesAll: null,
        employeeNumber: null,
        profit: null,
        indexFrom: '',
        fillType: '',
        rightParentCompany: null,
        assetAll: null,
        chiEmployeeNumber: null,
        capitalReturnRate: null
      },
      companyNameOption: [],
      currencyOption: [],
      rules: {
        financeYear: [{ required: true, message: '请选择财务年度', trigger: 'change' }],
        currency: [{ required: true, message: '请选择币种', trigger: 'change' }],
        income: [{ validator: checkNumber, trigger: 'change' }],
        profitParentCompany: [{ validator: checkNumber, trigger: 'change' }],
        liabilitiesAll: [{ validator: checkNumber, trigger: 'change' }],
        employeeNumber: [{ validator: checkNumber, trigger: 'change' }],
        capitalReturnRate: [{ validator: checkNumber, trigger: 'change' }],
        profit: [{ validator: checkNumber, trigger: 'change' }],
        rightParentCompany: [{ validator: checkNumber, trigger: 'change' }],
        assetAll: [{ validator: checkNumber, trigger: 'change' }],
        chiEmployeeNumber: [{ validator: checkNumber, trigger: 'change' }],
      },
      fillTypeOption: [
        {value: '手工录入', code: '01'},
        {value: '系统取数', code: '02'}
      ],
      profitOption: [],
      saveCmhkId: {
        cmhkId: ''
      },
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now() - 8.64e6 // 如果没有后面的-8.64e6就是不可以选择今天的
        }
      }
    }
  },
  mounted() {
    const key = this.$store.state.key.resourceCode
    if (key === '0') {
      this.tableOption.isShowOperate = false
    } else {
      this.tableOption.isShowOperate = true
    }
    this.$nextTick(() => {
      this.height = this.$refs.ref.offsetHeight
    })
    window.onresize = () => {
      this.height = this.$refs.ref.offsetHeight
    }
    Currency().then(res => {
      this.currencyOption = res.data.body
    })
    indexFrom().then(res => {
      this.profitOption = res.data.body
    })
  },
  destroyed(){
    window.onresize = null
  },
  methods: {
    remoteMethod(query) {
      const data = {
        chiName: query
      }
      queryCompanyByName(data).then(res => {
        if (res.data.code === '0') {
          this.companyNameOption = res.data.body
        }
      })
    },
    selectCompany(val) {
      this.saveCmhkId.cmhkId = val
      this.handleSearch()
      if (val === '') {
        this.tableData = []
      }
    },
    // 点击查询
    handleSearch() {
      if (this.searchData.cmhkId === '') {
        this.$message.error('请先选择公司')
      } else {
        queryFinanceList(this.searchData).then(res => {
          if (res.data.code === '0') {
            this.tableData = res.data.body.list
            this.total = res.data.body.total
          } else {
            return false
          }
        })
      }
    },
    // 点击新增
    handleAdd() {
      if (this.searchData.cmhkId === '') {
        this.$message.error('请先选择公司')
      } else {
        this.dialogVisibleAdd = true
        this.title = '新增'
      }
    },
    editRow(row) {
      this.dialogVisibleAdd = true
      this.title = '修改'
      const data = {
        uid: row.uid
      }
      querySingleFinance(data).then(res => {
        if (res.data.code === '0') {
          this.addForm = res.data.body
        }
      })
    },
    // 点击确定新增/修改
    handleSubmit(formName) {
      if (this.title === '新增') {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            const params = Object.assign(this.addForm, this.saveCmhkId)
            addFinanceList(params).then(res => {
              if (res.data.code === '0') {
                this.$message.success('新增成功')
                this.resetForm('form')
                this.dialogVisibleAdd = false
                this.handleSearch()
              } else {
                this.$message.error(res.data.message)
              }
            })
          } else {
            return false
          }
        })
      } else if (this.title === '修改') {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            const params = Object.assign(this.addForm, this.saveCmhkId)
            editFinanceList(params).then(res => {
              if (res.data.code === '0') {
                this.$message.success('修改成功')
                this.resetForm('form')
                this.dialogVisibleAdd = false
                this.handleSearch()
              } else {
                this.$message.error(res.data.message)
              }
            })
          } else {
            return false
          }
        })
      }
      
    },
    // 分页
    onTableChange(val) {
      this.searchData.page = val
      this.handleSearch()
    },
    handleCancle() {
      this.resetForm('form')
      this.dialogVisibleAdd = false
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
    
  }
}
</script>

<style lang="less" scoped>
  .app {
    height: 100%;
    .search-bar {
      width: calc(100% - 60px);
      background-color: #ffffff;
      margin-bottom: 10px;
      padding: 10px 30px;
    }
    .add-form {
      padding: 10px 20px;
      background-color: #ffffff;
    }
    .table-box {
      width: 100%;
      height: calc(100vh - 260px);
      .table {
        height: 100%;
      }
    }
    .table-box2 {
      width: 100%;
      height: calc(100vh - 230px);
      .table {
        height: 100%;
      }
    }
    .select-item {
      width: 100%;
    }
  }
</style>
